package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.ItemCriteria;
import com.leo.ibatis.demo.domain.Item;
import com.leo.ibatis.demo.service.ItemService;

public class ItemController extends MultiActionController {

	private ItemService itemService;

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ItemCriteria itemCriteria = new ItemCriteria();
		bind(request, itemCriteria);
		
		List<Item> itemList = itemService.listOnPage(itemCriteria);
		
		ModelAndView mav = new ModelAndView("item/list");
		mav.addObject("itemList", itemList);
		mav.addObject("itemCriteria", itemCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ItemCriteria itemCriteria = new ItemCriteria();
		bind(request, itemCriteria);
			
		String itemid = ServletRequestUtils.getRequiredStringParameter(request, "itemid");

		Item item = itemService.select(itemid);
		
		ModelAndView mav = new ModelAndView("item/detail");
		mav.addObject("item", item);
		mav.addObject("itemCriteria", itemCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String itemid = ServletRequestUtils.getRequiredStringParameter(request, "itemid");

		itemService.delete(itemid);
		
		return new ModelAndView("redirect:item.do");
	}

}
